package cn.wit.summit.chwan.net;

import com.google.gson.Gson;

import cn.wit.summit.chwan.models.dbmanager.NetResultCacheManager;
import cn.wit.summit.chwan.models.dto.ParmsBean;
import cn.wit.summit.chwan.net.okhttplib.HttpInfo;
import cn.wit.summit.chwan.net.okhttplib.NetConfigStr;
import cn.wit.summit.chwan.models.db.NetResultCacheTable;
import cn.wit.summit.chwan.net.okhttplib.interceptor.ExceptionInterceptor;
import cn.wit.summit.chwan.net.okhttplib.interceptor.ResultInterceptor;
import cn.wit.summit.chwan.utils.LogUtils;
import cn.wit.summit.chwan.utils.StringUtils;

/**
 * Created by Alan on 2018/3/29.
 */

public class HttpInterceptor {
    /**
     * 请求结果统一预处理拦截器
     * 该拦截器会对所有网络请求返回结果进行预处理并修改
     */
    public static cn.wit.summit.chwan.net.okhttplib.interceptor.ResultInterceptor ResultInterceptor = new ResultInterceptor() {
        @Override
        public HttpInfo intercept(HttpInfo info) throws Exception {
            String pn = "-1";
            String str = info.getParamJson();
            if (!StringUtils.isEmpty(str)) {
                ParmsBean parmsBean = new Gson().fromJson(str, ParmsBean.class);
                pn = parmsBean.getPn();
                if (!StringUtils.isEmpty(pn) && pn.equals("1")) {
                    LogUtils.e(">>>>pn:"+pn);
                    NetResultCacheManager.getInstance().insertData(info.getUrl(), str, info.getRetDetail());
                }
            } else {
                NetResultCacheManager.getInstance().insertData(info.getUrl(), str, info.getRetDetail());
            }
//            //请求结果预处理：可以进行GSon过滤与解析
//            NetResultCacheManager.getInstance().insertData(info.getUrl(), info.getRetDetail());
            return info;
        }
    };

    /**
     * 请求链路异常信息拦截器
     * 该拦截器会发送网络请求时链路异常信息进行拦截处理
     */
    public static cn.wit.summit.chwan.net.okhttplib.interceptor.ExceptionInterceptor ExceptionInterceptor = new ExceptionInterceptor() {
        @Override
        public HttpInfo intercept(HttpInfo info) throws Exception {
            NetResultCacheTable table = NetResultCacheManager.getInstance().getDataByUrl(info.getUrl(), info.getParamJson());
            if (table!=null && !StringUtils.isEmpty(table.getResponseStr())) {
                info.setRetCacheDetail(table.getResponseStr());
            }
            switch (info.getRetCode()){
                case HttpInfo.NonNetwork:
                    info.setRetMsg(NetConfigStr.STR_NonNetwork);
                    break;
                case HttpInfo.CheckURL:
//                    info.setRetMsg(NetConfigStr.STR_CheckURL_Two+"["+info.getNetCode()+"]");
                    info.setRetMsg(NetConfigStr.STR_CheckURL_Two);
                    break;
                case HttpInfo.ProtocolException:
//                    info.setRetMsg(NetConfigStr.STR_ProtocolException_Two+"["+info.getNetCode()+"]");
                    info.setRetMsg(NetConfigStr.STR_ProtocolException_Two);
                    break;
                case HttpInfo.CheckNet:
//                    info.setRetMsg(NetConfigStr.STR_CheckNet_Two+"["+info.getNetCode()+"]");
                    info.setRetMsg(NetConfigStr.STR_CheckNet_Two);
                    break;
                case HttpInfo.ConnectionTimeOut:
                    info.setRetMsg(NetConfigStr.STR_ConnectionTimeOut);
                    break;
                case HttpInfo.WriteAndReadTimeOut:
                    info.setRetMsg(NetConfigStr.STR_WriteAndReadTimeOut);
                    break;
                case HttpInfo.ConnectionInterruption:
                    info.setRetMsg(NetConfigStr.STR_ConnectionInterruption);
                    break;
            }
            return info;
        }
    };
}
